layui.use(['table', 'admin', 'ax', 'form', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    var fileRun = {
        tableId: "fileRunTable"
    };

    /**
     * 初始化表格的列
     */
    fileRun.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'fId', align: "center", hide: true, title: '文件ID'},
            {field: 'fName', align: "center", sort: true, title: '项目名称'},
            {field: 'fCategory', align: "center", sort: true, title: '项目类型'},
            {field: 'fAbstracts', align: "center", sort: true, title: '项目功能摘要', minWidth: 150},
            {field: 'fContent', align: "center", sort: true, title: '项目内容详情', minWidth: 150},
            {field: 'fTag', align: "center", sort: true, title: '项目技术栈'},
            {field: 'fDownCount', align: "center", sort: true, title: '下载次数'},
            {field: 'fUrl', align: "center", sort: true, title: 'FastDFS位置', minWidth: 150},
            // {field: 'fVideoUrl', align: "center", sort: true, title: '视频链接',minWidth: 150},
            {field: 'esState', align: "center", sort: true, title: 'ES增量情况'},
            {field: 'createUserName', align: "center", sort: true, title: '创建人姓名'},
            {field: 'showCreateTime', align: "center", sort: true, title: '上传时间'},
            {field: 'updateUserName', align: "center", sort: true, title: '操作人姓名'},
            {field: 'showUpdateTime', align: "center", sort: true, title: '更新时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    fileRun.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(fileRun.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    fileRun.openAddDlg = function () {
        func.open({
            type: 1,
            height: 1200,
            title: '添加文件信息',
            content: Feng.ctxPath + '/fileRun/add',
            tableId: fileRun.tableId
        });
    };

    /**
     * 点击编辑
     * @param data 点击按钮时候的行数据
     */
    fileRun.openEditDlg = function (data) {
        func.open({
            height: 600,
            title: '修改文件信息',
            content: Feng.ctxPath + '/fileRun/fileRun_update?fId=' + data.fId,
            tableId: fileRun.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    fileRun.exportExcel = function () {
        var checkRows = table.checkStatus(fileRun.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     */
    fileRun.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fileRun/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                    table.reload(fileRun.tableId);
                } else {
                    Feng.error("删除失败!" + data.message + "!");
                }
            });
            ajax.set("fId", data.fId);
            ajax.set("fUrl", data.fUrl);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    /**
     * 点击下载
     */
    fileRun.onDownload = function (data) {
        if(data.fUrl==null||data.fUrl==''){
            Feng.error("该位置无文件");
        }
        window.location.href = data.fUrl;
        var ajax = new $ax(Feng.ctxPath + "/fileRun/upCount", function (data) {
        });
        ajax.set("fId", data.fId);
        ajax.start();
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + fileRun.tableId,
        url: Feng.ctxPath + '/fileRun/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: fileRun.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        fileRun.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        fileRun.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        fileRun.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + fileRun.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            fileRun.openEditDlg(data);
        } else if (layEvent === 'delete') {
            fileRun.onDeleteItem(data);
        } else if(layEvent === 'download'){
            fileRun.onDownload(data);
        }
    });
});
